package br.com.reparoja.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.reparoja.entities.Brand;
import br.com.reparoja.entities.Equipment;
import br.com.reparoja.entities.User;
import br.com.reparoja.persistence.Factory;

public class InitServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.initialize();
		
		resp.setContentType("text/plain");
		resp.getWriter().println("Initializing database!");
	}
	
	public void initialize() throws ServletException {
		EntityManager em = Factory.get().createEntityManager();
		em.getTransaction().begin();
		
		//em.createNativeQuery("DROP TABLE User").executeUpdate();
		//em.createNativeQuery("DROP TABLE equipment_brand").executeUpdate();
		//em.createNativeQuery("DROP TABLE equipment").executeUpdate();
		//em.createNativeQuery("DROP TABLE brand").executeUpdate();

		
		List<Equipment> equips = new ArrayList<Equipment>();		
		List<Brand> brands = new ArrayList<Brand>();
		
		brands.add(new Brand("Brastemp"));
		brands.add(new Brand("Eletrolux"));
		brands.add(new Brand("Samsung"));
		brands.add(new Brand("LG"));
		brands.add(new Brand("Sony"));
		
		equips.add(new Equipment("Geladeira"));
		equips.add(new Equipment("Fogão"));
		equips.add(new Equipment("Microondas"));
		equips.add(new Equipment("Maquina de Lavar"));
		equips.add(new Equipment("Televisão"));
		equips.add(new Equipment("Home Theater"));

		brands.get(0).getEquipments().add(equips.get(0));
		brands.get(0).getEquipments().add(equips.get(1));
		brands.get(0).getEquipments().add(equips.get(2));
		
		brands.get(1).getEquipments().add(equips.get(0));
		brands.get(1).getEquipments().add(equips.get(1));

		brands.get(2).getEquipments().add(equips.get(3));
		brands.get(2).getEquipments().add(equips.get(4));

		brands.get(3).getEquipments().add(equips.get(4));

		brands.get(4).getEquipments().add(equips.get(4));
		brands.get(4).getEquipments().add(equips.get(5));

		for (Brand brand : brands) {
			em.persist(brand);
		}
		
		
		List<User> users = new ArrayList<User>();
		users.add(new User("Rogerio Ribeiro da Cruz", "rogeriorc@gmail.com"));
		users.add(new User("Thays Tagliaferri de Grazia", "thaystg@gmail.com"));
		
		for (User user : users) {
			em.persist(user);
		}
		/*
		
		for (Equipment equip : equips) {
			em.persist(equip);
		}
		
		//brands = em.createQuery("FROM Brand", Brand.class).getResultList();

		for (Brand brand : brands) {
			em.persist(brand);
			em.refresh(brand);
		}
		
		em.flush();
		em.clear();
*/
		em.getTransaction().commit();
		em.close();
	}
	
}
